package com.monitor.config;

import com.monitor.config.interceptor.LoginInterceptor;
import com.monitor.config.interceptor.OptionsInterceptor;
import com.monitor.web.common.token.JwtsUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.io.File;

@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
    @Value("${file.upload.root}")
    private String filePath;
    @Value("${debug}")
    private boolean debug;

    @Resource(name = "optionsInterceptor")
    private OptionsInterceptor optionsInterceptor;

    @Resource(name = "loginInterceptor")
    private LoginInterceptor loginInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/public/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/files/**").addResourceLocations("file:///" + filePath + File.separator);
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(false)
                .allowedOrigins("*")
                .allowedMethods(new String[]{"PUT", "DELETE", "GET", "POST", "OPTIONS"})
                .allowedHeaders("X-Requested-with", "Content-Type", "authorization", "jsonType")
                .exposedHeaders("Authorization", "X-Requested-With", "Content-Type")
                .maxAge(3600);
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(optionsInterceptor).addPathPatterns("/**");

        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/loginAction","/ykSync/*", "/files/**", "/imageCode");


    }

}
